package com.pro01.carcontrol.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/iot")
public class HuaweiIotController {

    private final Map<String, Object> latestData = new HashMap<>();
    private final ObjectMapper mapper = new ObjectMapper(); // 用于打印JSON

    @PostMapping("/receive")
    public ResponseEntity<String> receiveData(@RequestBody Map<String, Object> payload) {
        try {
            System.out.println("==== 收到华为云推送数据 ====");
            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(payload));

            // 1. 进入 notify_data
            Map<String, Object> notifyData = (Map<String, Object>) payload.get("notify_data");

            // 2. 进入 body
            Map<String, Object> body = (Map<String, Object>) notifyData.get("body");

            // 3. 提取 services 列表
            List<Map<String, Object>> services = (List<Map<String, Object>>) body.get("services");

            // 4. 提取第一个 service 的 properties
            Map<String, Object> firstService = services.get(0);
            Map<String, Object> properties = (Map<String, Object>) firstService.get("properties");

            // 5. 提取 sensor 数据
            Object illumination = properties.get("light");
            Object smoke        = properties.get("gas");
            Object pm25         = properties.get("pm25");

            System.out.println("【提取到】illumination = " + illumination + ", smoke = " + smoke + ", pm25 = " + pm25);

            // 6. 存入内存
            synchronized (latestData) {
                latestData.put("illumination", illumination);
                latestData.put("smoke", smoke);
                latestData.put("pm25", pm25);
                latestData.put("time", new Date());
            }

            System.out.println("【存入内存后的数据】" + toJson(latestData));
            return ResponseEntity.ok("✅ 数据解析成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("❌ 解析异常: " + e.getMessage());
        }
    }

    @GetMapping("/latest")
    public Map<String, Object> getLatestData() {
        synchronized (latestData) {
            Map<String, Object> result = new HashMap<>();
            result.put("illumination", latestData.get("illumination"));
            result.put("smoke", latestData.get("smoke"));
            result.put("pm25", latestData.get("pm25"));
            result.put("time", latestData.get("time"));
            System.out.println("【前端请求最新数据】" + toJson(result));
            return result;
        }
    }

    // 辅助打印JSON
    private String toJson(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (Exception e) {
            return obj == null ? "null" : obj.toString();
        }
    }

    @GetMapping("/test")
    public String test() {
        return "ngrok 接口通了";
    }
}